package com.lpit.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lpit.constant.Constant;
import com.lpit.dto.ArticleDto;
import com.lpit.pojo.Article;
import com.lpit.pojo.Channel;
import com.lpit.pojo.User;
import com.lpit.service.ArticleService;
import com.lpit.service.ChannelService;
import com.lpit.service.UserService;
import com.lpit.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 石智星
 * @date 2023/06/13
 * 文章审批跟驳回
 */
@RestController
@Slf4j
@RequestMapping("/article")
public class ArticleApprovalController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private UserService userService;
    @Autowired
    private ChannelService channelService;

    /**
     * 分页查询
     *
     * @param page     当前页
     * @param pageSize 每展示的条数
     * @return
     */
    @GetMapping("/list/{page}/{pageSize}")
    public Result<Page> pageQuery(@PathVariable Integer page, @PathVariable Integer pageSize, String name, String state) {
        log.info("当前页{}", page);
        log.info("每页数量{}", pageSize);
        //创建分页构造器
        Page<Article> articlePage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Article> articleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        articleLambdaQueryWrapper.like(name != null && !"".equals(name), Article::getArticleTitle, name);
        articleLambdaQueryWrapper.eq(state != null && !"".equals(state), Article::getArticleAuditStatus, state);
        //执行查询
        articleService.page(articlePage, articleLambdaQueryWrapper);
        //创建前端需要的分页构造器
        Page<ArticleDto> articleDtoPage = new Page<>();
        //对象拷贝
        BeanUtils.copyProperties(articlePage, articleDtoPage);
        //获取分页构造器里面的数据
        List<Article> articleList = articlePage.getRecords();
        List<ArticleDto> articleDtoList = new ArrayList<>();
        //遍历
        articleList.forEach(article -> {
            //创建前端需要的对象
            ArticleDto articleDto = new ArticleDto();
            //对象拷贝
            BeanUtils.copyProperties(article, articleDto);
            Integer userId = article.getUserId();
            //查询用户名字
            User user = userService.getById(userId);
            articleDto.setArticleAuthor(user.getName());

            Integer channelId = article.getChannelId();
            //查询频道名称
            Channel channel = channelService.getById(channelId);
            articleDto.setChannelName(channel.getChannelName());
            //统一放到集合里面
            articleDtoList.add(articleDto);
        });
        //将集合放入前端需要的构造器
        articleDtoPage.setRecords(articleDtoList);
        //返回结果
        return Result.success(articleDtoPage);
    }

    /**
     * 查看文章内容
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Article> query(@PathVariable Integer id) {
        //根据id查询文章
        Article article = articleService.getById(id);
        log.info("查看文章详情{}", article);
        //返回结果
        return Result.success(article);
    }

    /**
     * 文章审核通过
     *
     * @param id
     * @return
     */
    @PutMapping("/{id}")
    public Result<String> pass(@PathVariable Integer id) {
        //先根据id查询对象
        Article article = articleService.getById(id);
        //修改对象里面的属性
        article.setArticleAuditStatus(Constant.AUDITED);
        article.setArticleStatus(Constant.HAVE_PUBLISHED);
        //执行修改操作
        boolean update = articleService.updateById(article);
        //返回结果
        return update ? Result.success(Constant.MODIFY_SUCCESSFULLY) : Result.error(Constant.FAIL_TO_MODIFY);
    }

    /**
     * 文章审核驳回
     *
     * @param id
     * @return
     */
    @PutMapping("/reject/{id}")
    public Result<String> reject(@PathVariable Integer id) {
        //先根据id查询对象
        Article article = articleService.getById(id);
        //修改对象里面的属性
        article.setArticleAuditStatus(Constant.REJECTED);
        //执行修改操作
        boolean update = articleService.updateById(article);
        //返回结果
        return update ? Result.success(Constant.SUCCESSFUL_REJECTION) : Result.error(Constant.REJECTION_FAILURE);
    }
}
